/**
 * https://blog.csdn.net/weixin_40797306/article/details/81291048
 * 获取该周的所要显示的周和日期的对应数据，数据结构如下
 * var weekDay = {week: '',day: ''}
 * 参数：selectWeek  0为本周，数字代表前几周或者后几周，例如1是下一周
 */
function getWeekDayList(selectWeek,timeBeanx) {
  // 1.获取周一对应得时间
  // 2.用循环七次添加周一到周日对应得周几和几号
  var selectWeekTime = getCurrentTimeStamp() + (selectWeek * 7) * 24 * 60 * 60 * 1000
  //var mondayTime = selectWeekTime - (getWeekNumber(selectWeekTime) - 1) * 24 * 60 * 60 * 1000
  //js根据当前时间，获取本周一和本周日
  var dayy = new Date().getDay() > 0 ? new Date().getDay() : 7 // 表示当前是周几
  var mondayTime = selectWeekTime - (dayy - 1) * 24 * 60 * 60 * 1000
  //var SundayTime = new Date().getTime() + (7 - dayy) * 24 * 60 * 60 * 1000
  //console.log(timeBeanx);

  //https://www.bbsmax.com/A/VGzl3gVldb/
  //var now = new Date();
  //var year = now.getFullYear();
  //var month = now.getMonth() + 1;
  //var dayx = now.getDay(); //返回星期几的某一天;
  //var n = dayx == 0 ? -7 + 6 : -7 + (dayx - 1)
  //now.setDate(now.getDate() - n);
  //var date = now.getDate();
  //var s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date) + " 00:00:00";
  //var xiazhou = new Date(s).getTime();
  var d = new Date();//js下周一
  d.setDate(d.getDate() + (((1 + 7 - d.getDay()) % 7) || 7));//js下周一

  if(mondayTime>=d.getTime()){
    var benzhou = 2;
  }else{
    var benzhou = 1;
  }

  if(timeBeanx){
    var selectDay = timeBeanx.selectDay;
    if(mondayTime + selectDay * 24 * 60 * 60 * 1000<new Date().getTime()){
      selectDay = getCurrenrWeek();
    }
  }else{
    var selectDay = getCurrenrWeek();
  }

  var timeBean = {
    selectDay: selectDay,
    yearMonth: '',
    weekDayList: []
  }
 
  for (var i = 0; i < 7; i++) {
    var weekDay = {
      week: '',
      yanse: '',
      day: ''
    }

    if(new Date((mondayTime + i * 24 * 60 * 60 * 1000)).toDateString() === new Date().toDateString()){
      weekDay.week = '今天';
      weekDay.yanse = '';
    }else if((mondayTime + i * 24 * 60 * 60 * 1000) < Date.parse(new Date())){
      weekDay.week = '周'+getWeek(mondayTime + i * 24 * 60 * 60 * 1000);
      weekDay.yanse = 'yanse';
    }else{
      weekDay.week = '周'+getWeek(mondayTime + i * 24 * 60 * 60 * 1000);
      weekDay.yanse = '';
    }
    weekDay.nianyue = getYearMonth(mondayTime + i * 24 * 60 * 60 * 1000)
    weekDay.yue = getYuefen(mondayTime + i * 24 * 60 * 60 * 1000)
    weekDay.day = getMyDay(mondayTime + i * 24 * 60 * 60 * 1000)
    timeBean.weekDayList.push(weekDay)
  }
 
  timeBean.yearMonth = getYearMonth(selectWeekTime);
  timeBean.yearYue = getYuefen(selectWeekTime);
  //timeBean.monthDay = getYuefen(selectWeekTime)+"/"+getMyDay(mondayTime)+"-"+getYuefen(selectWeekTime)+"/"+getMyDay(mondayTime + 7 * 24 * 60 * 60 * 1000);
  //timeBean.selectDay = getCurrenrWeek();
  timeBean.ri = getYearMonth(mondayTime + selectDay * 24 * 60 * 60 * 1000)+"/"+getMyDay(mondayTime + selectDay * 24 * 60 * 60 * 1000);//选中日期
  timeBean.benzhou = benzhou;
  return timeBean;
}
 
 
//获取当前时间戳  --
function getCurrentTimeStamp() {
  var timestamp = new Date().getTime();
  return timestamp
}
 
//获取当前周几
function getCurrenrWeek() {
  var str = "6012345".charAt(new Date().getDay());
  return str;
}
 
//时间戳获得年月
function getYearMonth(res) {
  var time = new Date(res);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  return y + "/" + m;
}

//时间戳获得月
function getYuefen(res) {
  var time = new Date(res);
  var m = time.getMonth() + 1;
  return m;
}
 
//时间戳转几号
function getMyDay(res) {
  var time = new Date(res);
  var d = time.getDate();
  return d;
}
 
//时间戳转周几 
function getWeek(res) {
  var time = new Date(res);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  return "日一二三四五六".charAt(new Date(y + '/' + m + '/' + d).getDay());
}
 
//时间戳转周几 0123456  --
function getWeekNumber(res) {
  var time = new Date(res);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  return "0123456".charAt(new Date(y + '-' + m + '-' + d).getDay());
}

//把方法共享，让引用的地方可以调用
module.exports={
	getWeekDayList:getWeekDayList,
}